package com.itheima.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrdersettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrdersettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrdersettingService.class)
@Transactional
public class OrdersettingServiceImpl implements OrdersettingService {

    @Autowired
    private OrdersettingDao ordersettingDao;

    //添加Excel数据
    @Override
    public void upload(List<OrderSetting> orderSettingsList) {
        //判断list集合里是否有Excel数据,遍历全部数据
        if (orderSettingsList != null && orderSettingsList.size() > 0) {
            for (OrderSetting list : orderSettingsList) {
                //检查日期是否存在
                long count = ordersettingDao.findCountByOrderDate(list.getOrderDate());
                if (count > 0) {
                    //如果日期存在就覆盖数据
                    ordersettingDao.editOrderSettingByDate(list);
                } else {
                    //添加数据
                    ordersettingDao.upload(list);
                }
            }
        }
    }

    //展示预约信息
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        //设置一个月的天数
        String begin = date + "-1";
        String end = date + "-31";
        Map map = new HashMap();
        map.put("begin", begin);
        map.put("end", end);
        List<OrderSetting> list = ordersettingDao.getOrderSettingByMonth(map);
        //遍历每天的预约日期数存入集合
        List<Map> data = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            //每天的数据存入集合
            Map orderMap = new HashMap();
            //获取日期
            orderMap.put("date", orderSetting.getOrderDate().getDate());
            //获取预约人数
            orderMap.put("number", orderSetting.getNumber());
            //获取可预约人数
            orderMap.put("reservations", orderSetting.getReservations());
            data.add(orderMap);
        }
        return data;
    }

    //页面预约设置
    @Override
    public void editOrderSettingByDate(OrderSetting orderSetting) {
        //遍历预约数据
        long conut = ordersettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        //判断是否已存在,存在就覆盖,否则添加
        if (conut > 0) {
            ordersettingDao.editOrderSettingByDate(orderSetting);
        }else {
            ordersettingDao.upload(orderSetting);
        }
    }
}
